package com.ego.dubbo.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.commons.utils.IDUtils;
import com.ego.dubbo.service.TbItemDubboService;
import com.ego.mapper.TbItemDescMapper;
import com.ego.mapper.TbItemMapper;
import com.ego.mapper.TbItemParamItemMapper;
import com.ego.pojo.TbItem;
import com.ego.pojo.TbItemDesc;
import com.ego.pojo.TbItemExample;
import com.ego.pojo.TbItemParamItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.jute.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Title
 * @Author Lyg
 * @Description:
 */
@DubboService
public class TbItemDubboServiceImpl implements TbItemDubboService {
    @Autowired
    private TbItemMapper tbItemMapper;
    @Override
    public  PageInfo<TbItem> selectTbItemByPage(int page, int size) {
        //开始分页
        PageHelper.startPage(page,size);

        //创造条件对象
        TbItemExample tbItemExample = new TbItemExample();


        List<TbItem> tbItems = tbItemMapper.selectByExample(tbItemExample);

        //创建分页对象
        PageInfo<TbItem> tbItemPageInfo = new PageInfo<>(tbItems);

        return tbItemPageInfo;
    }

    @Override
    @Transactional(rollbackFor =Exception.class)
    public int updateItemStatusByIds(long[] ids, int status) throws DaoException {
        int index=0;
        Date date = new Date();
        for (Long id : ids) {
            TbItem tbItem = new TbItem();
            tbItem.setId(id);
            tbItem.setStatus((byte) status);
            tbItem.setUpdated(date);
            index+=tbItemMapper.updateByPrimaryKeySelective(tbItem);
        }

        if (index==ids.length){
            return 1;
        }
        // 让事务回滚
        throw new DaoException("批量修改失败");
    }
    @Autowired
    private TbItemDescMapper tbItemDescMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(TbItem tbItem, TbItemDesc tbItemDesc, TbItemParamItem tbItemParamItem) throws DaoException {
        int index=0;
        try {
            Date now = new Date();

            //设置创建时间和更新时间
            tbItem.setCreated(now);
            tbItem.setUpdated(now);
            //设置状态默认为上架状态
            tbItem.setStatus((byte)1);
            //新增商品
            index = tbItemMapper.insert(tbItem);
            if (index==1){
                //设置商品id
                tbItemDesc.setItemId(tbItem.getId());
                tbItemDesc.setCreated(now);
                tbItemDesc.setUpdated(now);

                //新增商品描述
                index = tbItemDescMapper.insert(tbItemDesc);
                if (index==1){
                    tbItemParamItem.setCreated(now);
                    tbItemParamItem.setUpdated(now);
                    tbItemParamItem.setItemId(tbItem.getId());

                    index = tbItemParamItemMapper.insert(tbItemParamItem);
                }
            }
            if(index == 1){
                return 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new DaoException("新增商品失败，请联系管理员");
        }
        throw new DaoException("新增商品失败，请联系管理员");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(TbItem tbItem, TbItemDesc tbItemDesc,TbItemParamItem tbItemParamItem) throws DaoException {
        int index=0;
        Date now = new Date();
        //设置商品修改时间
        tbItem.setUpdated(now);
        //设置商品描述的修改时间
        tbItemDesc.setUpdated(now);
        //设置商品描述要修改id
        tbItemDesc.setItemId(tbItem.getId());
        //商品规格参数的修改时间
        tbItemParamItem.setUpdated(now);
        try {
            //修改商品
            index= tbItemMapper.updateByPrimaryKeySelective(tbItem);

            if (index==1){
                index= tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);

                if (index==1){
                    index= tbItemParamItemMapper.updateByPrimaryKeySelective(tbItemParamItem);

                }
                    if (index==1){
                        return index;
                     }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new DaoException("修改商品失败，请联系管理员");
        }
        throw new DaoException("修改商品失败，请联系管理员");
    }

    @Override
    public List<TbItem> selectByIds(List<Long> ids) {
        TbItemExample tbItemExample = new TbItemExample();
        tbItemExample.createCriteria().andIdIn(ids);

        List<TbItem> tbItems = tbItemMapper.selectByExample(tbItemExample);


        return tbItems;
    }

    @Override
    public TbItem selectById(Long id) {
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(id);
        return tbItem;
    }
}
